<template>
  <div class="employee">
    <!-- 组件 -->
    <pagetools :showleft="true">
      <template #left>员工管理</template>
      <template #right>
        <el-button @click="toTmport" type="primary" size="mini">导入</el-button>
        <el-button type="primary" size="mini" @click="openAdd"><i class="ei-icon-plus"></i>新增员工</el-button>
      </template>
    </pagetools>
    <!-- 表格区域 -->
    <el-card class="mycard">
      <div class="box">
        <!-- 表格 -->
        <el-table :data="list">
          <el-table-column width="150" label="序号" type="index"> </el-table-column>
          <el-table-column width="150" label="头像">
            <template slot-scope="scope">
              <img
                class="myImg"
                v-errorImg="defaultImg"
                :src="scope.row.staffPhoto"
                alt=""
                @click="openPreview(scope.row.staffPhoto)"
              />
            </template>
          </el-table-column>
          <el-table-column width="150" prop="username" label="姓名"> </el-table-column>
          <el-table-column width="150" prop="mobile" label="手机号"> </el-table-column>
          <el-table-column width="150" prop="workNumber" label="工号"> </el-table-column>
          <el-table-column width="150" label="聘用形式">
            <template slot-scope="scope">
              {{ scope.row.formOfEmployment | pinyong }}
            </template>
          </el-table-column>
          <el-table-column width="150" prop="departmentName" label="部门"> </el-table-column>
          <el-table-column width="150" label="入职时间">
            <template slot-scope="scope">
              {{ scope.row.timeOfEntry | dateTime }}
            </template>
          </el-table-column>
          <el-table-column width="150" label="是否在职">
            <template>
              <span>在职</span>
            </template>
          </el-table-column>
          <el-table-column width="150" label="状态">
            <template>
              <span>可用</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="250" fixed="right">
            <template slot-scope="scope">
              <el-button :disabled="'employees_check' | hasRight" type="text" @click="toDetail(scope.row)">
                查看
              </el-button>
              <el-button :disabled="'employees_trun' | hasRight" type="text"> 转正 </el-button>
              <el-button :disabled="'employees_job' | hasRight" type="text"> 调岗 </el-button>
              <el-button :disabled="'employees_role' | hasRight" type="text" @click="openRole(scope.row.id)">
                角色
              </el-button>
              <el-button :disabled="'employees_del' | hasRight" type="text"> 删除 </el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page"
          :page-sizes="pagesizes"
          :page-size="pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </el-card>
    <!-- 新增面板 -->
    <mypanel :dialogShow.sync="dialogShow" @updateDate="getEmpData"></mypanel>
    <!-- 预览图片 -->
    <el-dialog title="预览图片" :visible.sync="previewShow">
      <!-- <img :src="previewImg" /> -->
      <canvas ref="mycanvas"></canvas>
    </el-dialog>
    <!-- 角色面板 -->
    <el-dialog title="分配角色" :visible.sync="roleShow">
      <el-checkbox-group v-model="roleList">
        <el-checkbox v-for="(item, index) in roleArr" :key="index" :label="item.id"> {{ item.name }}</el-checkbox>
      </el-checkbox-group>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submit">提交</el-button>
        <el-button @click="roleShow = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getListApi } from '@/api/setting'
import { getEmpDataApi, getInfoByIdApi, setEmpRoleApi } from '@/api/employees'
import QrCode from 'qrcode'
export default {
  data() {
    return {
      list: [],
      // 选中页码
      page: 1,
      pagesizes: [10, 15, 20, 30],
      // 选中页容量
      pagesize: 10,
      total: 0,
      // 控制面板显示隐藏
      dialogShow: false,
      defaultImg: require('@/assets/common/bigUserHeader.png'),
      // 控制面板显示隐藏
      previewShow: false,
      // 保存预览图片
      previewImg: '',
      // 控制狠角色慢板显示与隐藏
      roleShow: false,
      // 选中角色的列表
      roleList: [],
      // 角色列表
      roleArr: [],
      // 当前操作用户的id
      currentId: ''
    }
  },
  methods: {
    // 提交员工角色
    async submit() {
      await setEmpRoleApi({
        id: this.currentId,
        roleIds: this.roleList
      })
      this.$message.success('修改成功')
      this.roleShow = false
      // 更新列表
      this.getEmpData()
    },
    // 控制角色面板
    async openRole(id) {
      this.roleShow = true
      this.currentId = id
      const res = await getListApi()
      //  console.log(res);
      this.roleArr = res.data.data.rows
      // 得到当前点击员工的角色信息
      const resEmp = await getInfoByIdApi(id)
      // console.log(resEmp)
      this.roleList = resEmp.data.data.roleIds
    },
    // 打开预览面板,保存预览图片
    openPreview(img) {
      this.previewShow = true
      this.$nextTick(() => {
        QrCode.toCanvas(this.$refs.mycanvas, img, {
          width: 200,
          height: 200
        })
      })
    },
    // 跳转到详情页
    toDetail(row) {
      this.$router.push(`/employees/detail/${row.id}`)
    },
    // 跳转到import页面
    toTmport() {
      this.$router.push('/employees/import')
    },
    // 打开面板
    openAdd() {
      this.dialogShow = true
    },
    // 获取员工列表
    async getEmpData() {
      const res = await getEmpDataApi({
        page: this.page,
        size: this.pagesize
      })
      // console.log(res)
      this.list = res.data.data.rows
      this.total = res.data.data.total
    },
    // 页容量改变触发
    handleSizeChange(size) {
      this.pagesize = size
      this.getEmpData()
    },
    // 当前页改变触发
    handleCurrentChange(page) {
      this.page = page
      this.getEmpData()
    }
  },
  created() {
    this.getEmpData()
  },
  components: {
    mypanel: () => import('./components/mypanel.vue')
  }
}
</script>

<style lang="scss" scoped>
.employee {
  margin: 20px;
  .mycard {
    margin-top: 20px;
    ::v-deep .el-pagination {
      text-align: right;
      margin-top: 20px;
    }
  }
  .myImg {
    width: 30px;
    height: 30px;
    border-radius: 50%;
  }
}
</style>
